<template>
  <div ref="bar" class="main"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: "Bar_Echart",
  props:['activeName','incomeData','payoutData'],
  data(){
    return{
      color:'',
      chart:null,
    }
  },
  mounted() {
    this.chart=echarts.init(this.$refs.bar)
    const options= {
      title:{
        text:'支出统计  总金额:￥0',
      },
      tooltip: {
        trigger: 'axis',
        formatter: this.activeName == 'income'?'{b} <br/> 总收入: {c}':'{b} <br/> 总支出: {c}',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: [],
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          color:'#08a2ff',
          barWidth: '60%',
          data: [],
          label:{
            show: true,
            position: 'top'
          }
        }
      ]
    };
    this.chart.setOption(options);
  },
  computed:{
    incomeSum(){
      let val=0;
      this.incomeData[0].forEach((item)=>{
        val=val+item;
      })
      //解决浮点数精度问题，保留两位小数
      return Math.floor(val * 100) / 100;
    },
    payoutSum(){
      let val=0;
      this.payoutData[0].forEach((item)=>{
        val=val+item;
      })
      //解决浮点数精度问题，保留两位小数
      return Math.floor(val * 100) / 100;
    }
  },
  watch:{
    incomeData:{
      handler(){
        this.chart.setOption({
          title:{
            text:this.activeName=='income'? '收入统计  总金额:￥'+this.incomeSum:'支出统计  总金额:￥'+this.payoutSum,
          },
          xAxis:{
            data:this.activeName=='income'?this.incomeData[1]:this.payoutData[1],
          },
          series:[{
            data:this.activeName=='income'?this.incomeData[0]:this.payoutData[0],
            color:this.activeName=='income'?'#34ab01':'#08a2ff'
          }]
        })
      },
      deep: true,
    },
    payoutData:{
      handler(){
        this.chart.setOption({
          title:{
            text:this.activeName=='income'? '收入统计  总金额:￥'+this.incomeSum:'支出统计  总金额:￥'+this.payoutSum,
          },
          xAxis:{
            data:this.activeName=='income'?this.incomeData[1]:this.payoutData[1],
          },
          series:[{
            data:this.activeName=='income'?this.incomeData[0]:this.payoutData[0],
            color:this.activeName=='income'?'#34ab01':'#08a2ff'
          }]
        })
      },
      deep: true,
    },
    activeName:{
      handler(){
        this.chart.setOption({
          title:{
            text:this.activeName=='income'? '收入统计  总金额:￥'+this.incomeSum:'支出统计  总金额:￥'+this.payoutSum,
          },
          xAxis:{
            data:this.activeName=='income'?this.incomeData[1]:this.payoutData[1],
          },
          series:[{
            data:this.activeName=='income'?this.incomeData[0]:this.payoutData[0],
            color:this.activeName=='income'?'#34ab01':'#08a2ff'
          }],
          tooltip: {
            trigger: 'axis',
            formatter: this.activeName == 'income'?'{b} <br/> 总收入: {c}':'{b} <br/> 总支出: {c}',
            axisPointer: {
              type: 'shadow'
            }
          },
        })
      },
      deep: true,
    },
  }
}
</script>

<style scoped>
.main{
  width: 100%;
  height: 500px;
}
</style>